home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / CLISUBR.C < prev    next >
Text File  |  1993-08-09  |  6KB  |  275 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "config.h"
  4. #include "socket.h"
  5. #include "session.h"
  6. #include "commands.h"
  7. #include "remote.h"
  8. #include "tcp.h"            /* for kick() */
  9. #include "files.h"
  10. #include "dirutil.h"
  11. #include "clients.h"
  12. #include "ftp.h"
  13. #include "smtp.h"
  14.  
  15. #ifdef DNS
  16. #include "domain.h"
  17. #endif
  18.  
  19. #ifdef LZW
  20. #include "lzw.h"
  21. #endif
  22.  
  23. #ifdef CONVERS
  24. #include "convers.h"
  25. #endif
  26.  
  27. #ifdef POP
  28. #include "pop.h"
  29. #endif
  30.  
  31. #ifdef NNTP
  32. #include "nntp.h"
  33. #endif
  34.  
  35. static int CLoginwait = 0;
  36. struct Server *Server = NULLSERVER;
  37.  
  38.  
  39. /* Attempt to log in the remote system, which is specified by 'host' */
  40. int16
  41. cli_login(int16 protocol,void *ccb)
  42. {
  43.   FILE *fp;
  44.   char cp[LINELEN];
  45.   int16 perms = 1;
  46.  
  47.   semwait(&CLoginwait,1);
  48.  
  49.   sprintf(cp,"%s/%s.cfg",EtcRoot,tcp_port(protocol));
  50.  
  51.   if((fp = Fopen(cp,READ_TEXT,0,1)) != NULLFILE) {
  52.     int def = FALSE;
  53.     char *def_arg2 = NULLCHAR, *def_arg3 = NULLCHAR, *def_arg4 = NULLCHAR;
  54.     char arg1[40], arg2[40], arg3[40], arg4[40];
  55.  
  56.     for( ; ;) {
  57.       memset(cp,0,LINELEN);
  58.  
  59.       fgets(cp,LINELEN,fp);
  60.  
  61.       /* Comment line */
  62.       if(*cp == '#') {
  63.         continue;
  64.       }
  65.       *arg1 = '\0';
  66.       *arg2 = '\0';
  67.       *arg3 = '\0';
  68.       *arg4 = '\0';
  69.  
  70.       /* there must be at least 3 arguments ... */
  71.       sscanf(cp,"%s %s %s %s",arg1,arg2,arg3,arg4);
  72.  
  73.       if(strcmp(arg1,"default") == 0) {
  74.         if(def_arg2) xfree(def_arg2);
  75.         if(def_arg3) xfree(def_arg3);
  76.         if(def_arg4) xfree(def_arg4);
  77.         if(arg2) def_arg2 = strxdup(arg2);
  78.         if(arg3) def_arg3 = strxdup(arg3);
  79.         if(arg4) def_arg4 = strxdup(arg4);
  80.         def = TRUE;
  81.       } else {
  82.         switch(protocol) {
  83.           case IPPORT_FTP: {
  84.             struct ftpcli *ftp = ccb;
  85.  
  86.             if(strcmp(arg1,ftp->buf) == 0) {
  87.               ftp->username = strxdup(arg2);
  88.               ftp->password = strxdup(arg3);
  89.               goto found;
  90.             }
  91.             if(feof(fp)) {
  92.               if(def == TRUE) {
  93.                 ftp->username = strxdup(def_arg2);
  94.                 ftp->password = strxdup(def_arg3);
  95.                 goto found;
  96.               }
  97.               perms = 0;
  98.               goto found;
  99.             }
  100.             break;
  101.           }
  102. #ifdef POP
  103.           case IPPORT_POP: {
  104.             struct Server *pop = ccb;
  105.  
  106.             if(strcmp(arg1,pop->name) == 0) {
  107.                 pop->arg1 = strxdup(arg2);
  108.                 pop->arg2 = strxdup(arg3);
  109.                 pop->arg3 = strxdup(arg4);
  110.                 goto found;
  111.             }
  112.             if(feof(fp)) {
  113.               if(def == TRUE) {
  114.                 pop->arg1 = strxdup(def_arg2);
  115.                 pop->arg2 = strxdup(def_arg3);
  116.                 pop->arg3 = strxdup(def_arg4);
  117.                 goto found;
  118.               }
  119.               perms = 0;
  120.               goto found;
  121.             }
  122.             break;
  123.           }
  124. #endif
  125. #ifdef notusedyet
  126.           case IPPORT_TTYLINK:
  127.           case IPPORT_TELNET: {
  128.             struct telnet *telcli = ccb;
  129.  
  130.             if(stricmp(arg1,telcli->username) == 0) {
  131.               telcli->username = strxdup(arg2);
  132.               telcli->password = strxdup(arg3);
  133.               goto found;
  134.             }
  135.             if(feof(fp)) {
  136.               if(def == TRUE) {
  137.                 telcli->username = strxdup(def_arg2);
  138.                 telcli->password = strxdup(def_arg3);
  139.                 goto found;
  140.               }
  141.               perms = 0;
  142.               goto found;
  143.             }
  144.             break;
  145.           }
  146. #endif
  147. #ifdef CONVERS
  148.           case IPPORT_XCONVERS:
  149.           case IPPORT_CONVERS:
  150. #endif
  151.           case IPPORT_ECHO:
  152.           case IPPORT_DISCARD:
  153.           case IPPORT_SMTP:
  154.           case IPPORT_FINGER:
  155. #ifdef DNS
  156.           case IPPORT_DOMAIN:
  157. #endif
  158. #ifdef UDP
  159.           case IPPORT_REMOTE:
  160. #endif
  161. #ifdef NNTP
  162.           case IPPORT_NNTP:
  163. #endif
  164.           {
  165.             goto found;
  166.           }
  167.         }
  168.         if(feof(fp)) {
  169.           perms = 0;
  170.           goto found;
  171.         }
  172.       }
  173.     }
  174. found:
  175.     Fclose(fp);
  176.  
  177.     if(def_arg2) xfree(def_arg2);
  178.     if(def_arg3) xfree(def_arg3);
  179.     if(def_arg4) xfree(def_arg4);
  180.   }
  181.   semrel(&CLoginwait);
  182.  
  183.   /* Finally return the permission flag */
  184.   return perms;
  185. }
  186.  
  187.  
  188. struct Server *
  189. addserver(int16 protocol,char *host)
  190. {
  191.     struct Server *np = NULLSERVER;
  192.     int32 addr;
  193.  
  194.     if((addr = resolve(host)) == 0) {
  195.         tprintf(Badhost,host);
  196.     } else {
  197.         for(np = Server; np != NULLSERVER; np = np->next) {
  198.             if(np->address == addr && np->protocol == protocol) {
  199.                 return np;
  200.             }
  201.         }
  202.         if(np == NULLSERVER) {
  203.             np = mxallocw(sizeof(struct Server));
  204.             np->name = strxdup(host);
  205.             np->address = addr;
  206.             np->busy = FALSE;
  207.             np->protocol = protocol;
  208.  
  209.             np->next = Server;
  210.             Server = np;
  211.         }
  212.     }
  213.     return np;
  214. }
  215.  
  216. struct Server *
  217. dropserver(int16 protocol,char *host)
  218. {
  219.     struct Server *np, *npprev = NULLSERVER;
  220.     int32 addr;
  221.  
  222.     if((addr = resolve(host)) == 0) {
  223.         tprintf(Badhost,host);
  224.     } else {
  225.         for(np = Server; np != NULLSERVER; npprev = np, np = np->next) {
  226.             if(np->address == addr && np->protocol == protocol) {
  227.                 if(np->busy) {
  228.                     tprintf("Entry for %s is active\n",inet_ntoa(np->address));
  229.                     return NULLSERVER;
  230.                 }
  231.                 if(npprev != NULLSERVER) {
  232.                     npprev->next = np->next;
  233.                 } else {
  234.                     Server = np->next;
  235.                 }
  236.                 xfree(np->name);
  237.                 if(np->arg1) xfree(np->arg1);
  238.                 if(np->arg2) xfree(np->arg2);
  239.                 if(np->arg3) xfree(np->arg3);
  240.                 if(np->arg4) xfree(np->arg4);
  241.                 xfree(np);
  242.             }
  243.         }
  244.     }
  245.     return NULLSERVER;
  246. }
  247.  
  248. void
  249. listserver(int16 protocol,char *host,char *title)
  250. {
  251.     struct Server *np;
  252.  
  253.     tprintf("Address          Status   %s\n",(*host == 'x') ? "" : title);
  254.  
  255.     for(np = Server; np != NULLSERVER; np = np->next) {
  256.         if(*host == 'x' || np->protocol == protocol) {
  257.             tprintf("%-17s%-9s",
  258.               inet_ntoa(np->address),
  259.               np->busy ? "Busy" : "Idle");
  260.             switch(protocol) {
  261.             case IPPORT_POP:
  262.                 tprintf("%-10s%s",
  263.                     np->arg1 ? np->arg1 : "",
  264.                     np->arg2 ? np->arg2 : "");
  265.                 break;
  266.             case IPPORT_NNTP:
  267.                 tputs(np->arg1);
  268.                 break;
  269.             }
  270.             tputs("\n");
  271.         }
  272.     }
  273.     return;
  274. }
  275.